package com.tsky.yyj.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tsky.casing.QueryBean;
import com.tsky.casing.action.BackAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.yyj.entity.Seller;
import com.tsky.yyj.json.YYJJsonFormat;
import com.tsky.yyj.manager.BaseManager;

@Component("sellerAction")
@Scope("prototype")
public class SellerAction extends BackAction {
	@Autowired
	private BaseManager entityManager;

	/* bean properties */
	private String ids;
	private String id;// 序号
	private String code;// 用户名
	private String name;// 商户名称
	private String contact;// 联系人
	private String license;// 营业执照
	
	// 封装上传文件域的属性
	private File image;
	// 封装上传文件类型的属性
	private String imageContentType;
	// 封装上传文件名的属性
	private String imageFileName;
	// 接受依赖注入的属性
	private String savePath;

	public String upload() {
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			// 建立文件输出流
			System.out.println(getSavePath());
			fos = new FileOutputStream(getSavePath() + "\\"
					+ getImageFileName());
			// 建立文件上传流
			fis = new FileInputStream(getImage());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			e.printStackTrace();
		} finally {
			close(fos, fis);
		}
		return SUCCESS;
	}
	
	public void grid() {
		QueryBean grid = new QueryBean();
		BeanUtil.copyProperties(grid, this);

		entityManager.query(grid, Seller.class);

		String jsonData = YYJJsonFormat.sellerToJson(grid);
		ActionUtil.ajaxWrite(jsonData);
	}

	public String add() {
		upload();
		
		Seller entity = new Seller();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());
		String image = getImageFileName();
		entity.setLicense(image);

		boolean result = entityManager.saveEntity(entity);
		ActionUtil.setToEL("result", result);
		ActionUtil.setToEL("image", image);
//		ActionUtil.ajaxWrite(result);
		
		return SUCCESS;
	}

	public void delete() {
		String[] idset = ids.split(",");
		boolean result = entityManager.delEntity(Seller.class, idset);
		ActionUtil.ajaxWrite(result);
	}

	public String query() {
		Seller entity = entityManager.getEntity(Seller.class, id);
		ActionUtil.setToEL("sellerEnt", entity);
		return "query";
	}

	public String update() {
		upload();
		Seller entity = entityManager.getEntity(Seller.class, id);
		BeanUtil.copyProperties(entity, this);
		String image = getImageFileName();
		entity.setLicense(image);

		boolean result = entityManager.updateEntity(entity);
		ActionUtil.setToEL("result", result);
		ActionUtil.setToEL("image", image);
		
		return "edit";
	}

	public BaseManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(BaseManager entityManager) {
		this.entityManager = entityManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getLicense() {
		return license;
	}

	public void setLicense(String license) {
		this.license = license;
	}

	public String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		String fileName = imageFileName;
		String ext = imageFileName.substring(imageFileName.indexOf("."),
				imageFileName.length());
		this.imageFileName = BeanUtil.getID() + ext;
	}

	private void close(FileOutputStream fos, FileInputStream fis) {
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				System.out.println("FileInputStream关闭失败");
				e.printStackTrace();
			}
		}
		if (fos != null) {
			try {
				fos.close();
			} catch (IOException e) {
				System.out.println("FileOutputStream关闭失败");
				e.printStackTrace();
			}
		}
	}
}
